﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VirtualTournaments.DataAccess;
using VirtualTournaments.DataAccess.EntityManagers;

namespace VirtualTournaments.Services.Tournaments
{
    public class PlayersService
    {

        private static List<Player> createPlayersForTeam()
        {
            List<Player> result = new List<Player>();
            result.Add(new Player("Gabriel Batistuta", "Argentina", "FORWARS", "60", "90", "100", "65"));
            result.Add(new Player("Andres Laspada", "Argentina", "DEFENDER", "60", "60", "60", "60"));
            result.Add(new Player("Eber Ludueña", "Argentina", "DEFENDER", "10", "10", "100", "10"));
            result.Add(new Player("Esteban Cambiasso", "Argentina", "MIDFIELDER", "10", "10", "100", "10"));
            result.Add(new Player("Leonel Messi", "Argentina", "MIDFIELDER", "100", "100", "100", "100"));
            result.Add(new Player("Toni Meola", "Argentina", "GOALKEEPER", "70", "60", "80", "60"));

            return result;
        }

        public static Player[] GetPlayersByTeamName(string teamName)
        {

            List<Player> result = new List<Player>();
            ITeamsManager teamsManager = DataAccessManager.EntityManagersFactory.GetTeamsManager();

            Business.Tournaments.Team team = teamsManager.GetByName(teamName);
            if (team == null)
            {
                return result.ToArray();
            }
      
            foreach (var player in team.Players)
            {
                result.Add(Bussiness2ServiceConversor.ConvertPlayer(player));
            }

            return result.ToArray();
        }


        public static Player[] GetPlayersByTeamNameAndTournamentName(string teamName, string tournamentName)
        {

            List<Player> result = new List<Player>();
            ITournamentsManager tournamentsManager = DataAccessManager.EntityManagersFactory.GetTournamentsManager();
            Business.Tournaments.Tournament businessTournament= tournamentsManager.GetByExactName(tournamentName);

            if (businessTournament == null)
                return result.ToArray();

            foreach (var team in businessTournament.Teams)
            {
                if (team.Name == teamName)
               {
                    foreach (var player in team.Players) result.Add(Bussiness2ServiceConversor.ConvertPlayer(player));
                }
            }


            result = createPlayersForTeam();

            return result.ToArray();

        }

        

    }
}
